/**
as3app - An open-source ActionScript 3.0 Library developed to assist 
in creating simple visual controls/data binding/asset loading for the
Flash player platform.

http://code.google.com/p/as3app/

Copyright (c) 2011 M. K. Chowdhury, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package as3app.binding 
{
	/**
	 * ...
	 * @author ...
	 */
	public class Binding
	{
		
		
		protected var _key:String = "as3app.Binding.Binding";
		public function get Key():String { return _key; }
		
		protected var _controls:Array = new Array();
		
		
		protected var _value:Object = null;
		public function get Value():Object { return _value; }
		public function set Value(value:Object) 
		{ 
			_value = value;

			for(var i:int = 0; i < _controls.length;++i )
			{
				var obj:Object = _controls[i].Control;
				var properties:Array = _controls[i].Properties as Array;
				
				for (var j:int = 0; j < properties.length-1;++j )
				{
					obj = obj[String(properties[j])] as Object;
				}
				obj[String(properties[properties.length-1])] = _value;
				//trace(obj[properties.length - 1]);

			}
		}
		
		public function SetValue(control:Object)
		{
			for (var i:int = 0; i < _controls.length;++i ) 
				if (_controls[i].Control == control)
				{
					var obj:Object = _controls[i].Control;
					var properties:Array = _controls[i].Properties as Array;
				
					for (var j:int = 0; j < properties.length;++j )
					{
						obj = obj[String(properties[j])] as Object;
					}
					Value = obj;
					
					return;
				}
		}
		
		
		
		public function Exists(control:Object):Boolean
		{
			for each(var item:Object in _controls) if (item.Control == control) return true;
			return false;
		}
		
		
		public function Add(control:Object, targetedProperty:Array, isSource:Boolean)
		{
			var object:Object = new Object();
			
			object.Control = control;
			object.Properties = targetedProperty;
			object.IsSource = isSource;
			
			_controls.push(object);
			
		}
		
		
		public function Remove(control:Object)
		{
			for (var i:int = 0; i < _controls.length;++i ) 
				if (_controls[i].Control == control)
				{
					_controls.splice(i, 1);
					return;
				}
			
		}
		
		
		
		public function Binding(key:String)
		{
			_key = key;
		}
	}

}